package com.zhongwang.mms.module.basic.service;

import com.zhongwang.mms.gen.entity.TDept;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.gen.entity.TEmployExample;
import com.zhongwang.mms.gen.entity.TWarehouseInfo;
import com.zhongwang.mms.module.basic.dao.TDeptDao;
import com.zhongwang.mms.module.basic.dao.TEmployDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 员工 服务层
 * @Author: China.wsg
 * @Date: 2020/06/03 10:40
 */
@Service
public class TEmployService {

    @Autowired
    private TEmployDao employDao;
    @Autowired
    private TDeptDao deptDao;

    /**
     * 取得员工
     *
     * @param employ 检索条件
     * @return
     */
    public List<TEmploy> findEmploy(TEmploy employ) {

        TEmployExample ex = new TEmployExample();
        ex.setOrderByClause("emp_order_no asc");
        TEmployExample.Criteria criteria = ex.createCriteria();
        criteria.andEmpStatusNotEqualTo(0);  // 状态：0=删除 1=可用 2=失效

        if (employ.getEmpName() != null && !"".equals(employ.getEmpName())) {
            criteria.andEmpNameLike("%" + employ.getEmpName() + "%");
        }
        if (employ.getEmpStatus() != null && !"".equals(employ.getEmpStatus())) {
            criteria.andEmpStatusEqualTo(employ.getEmpStatus());
        }

        return employDao.selectByExample(ex);
    }

    /**
     * 保存员工信息
     *
     * @param employ 员工信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(TEmploy employ) {
        // 取得部门名称
        TDept dept = deptDao.selectByPrimaryKey(employ.getEmpDepId());
        if (dept != null){
            employ.setEmpDepName(dept.getDepName());
        }
        int row = employDao.insert(employ);
        return row;
    }

    /**
     * 按名称名查询员工
     *
     * @param name 员工
     * @return 员工信息
     */
    public TEmploy findEmployByName(String name) {
        TEmployExample ex = new TEmployExample();
        TEmployExample.Criteria criteria = ex.createCriteria();
        criteria.andEmpNameEqualTo(name);
        criteria.andEmpStatusNotEqualTo(0);

        List<TEmploy> employs = employDao.selectByExample(ex);
        if (employs != null && employs.size() > 0) {
            return employs.get(0);
        }
        return null;
    }

    /**
     * 修改员工
     *
     * @param employ 员工信息
     * @return 修改行数
     */
    public String del(TEmploy employ) {

        int row = employDao.updateByPrimaryKeySelective(employ);

        // 删除下级
        TEmploy tEmploy = new TEmploy();
        tEmploy.setEmpStatus(0);
        TEmployExample ex = new TEmployExample();
        TEmployExample.Criteria criteria = ex.createCriteria();
        criteria.andEmpPidEqualTo(employ.getEmpId());
        employDao.updateByExampleSelective(tEmploy, ex);

        return "";
    }

    /**
     * 根据主键查询员工
     *
     * @param id 主键
     * @return 员工
     */
    public TEmploy findEmployById(Integer id) {
        return employDao.selectByPrimaryKey(id);
    }

    /**
     * 修改员工
     *
     * @param employ 员工信息
     * @return 影响行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int updatEmploy(TEmploy employ) {
        // 取得部门名称
        TDept dept = deptDao.selectByPrimaryKey(employ.getEmpDepId());
        if (dept != null){
            employ.setEmpDepName(dept.getDepName());
        }
        int row = employDao.updateByPrimaryKeySelective(employ);
        return row;
    }


}
